package com.faiz.example.springtest.controller;

import com.faiz.example.springtest.Utils.SnowWorker;
import com.faiz.example.springtest.entity.Teacher;
import com.faiz.example.springtest.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api("mybatis学习教程")
@RestController
public class MybatisController {
    private final TeacherService teacherService;

    @Autowired
    public MybatisController(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    @ApiOperation(value = "获取所有教师")
    @GetMapping(path = {"/teachers", "/"})
    public List<Teacher> getAllTeacher() {
        return teacherService.selectAllTeacher();
    }

    @ApiOperation(value = "添加教师")
    @PostMapping("/addTeacher")
    public Teacher upsertTeacher(@RequestBody Teacher teacher) {
        teacher.setId(SnowWorker.generateId());
        return teacherService.upsertTeacher(teacher);
    }
}
